// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";

Vue.config.productionTip = false;

import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  //state 相当于我们的data
  state: {
    count: 0,
    msg: "good morning!",
    list: [1, 2, 3, 4, 5, 6, 7, 8]
  },

  //相当于我们的methods
  mutations: {
    increment(state, payload) {
      if (payload) {
        state.count += payload * 1;
      } else {
        state.count++;
      }
    },
    addMsg(state, payload) {
      state.msg += payload;
    }
  },

  //actions异步的methods
  actions: {
    asyncIncrement(context) {
      //context 就是 store
      setTimeout(() => {
        console.log(context);
        context.commit("increment");
      }, 2000);
    },
    asyncAdd(ctx, payload) {
      setTimeout(() => {
        //  console.log();

        ctx.commit("increment", payload);
      }, 1000);
    }
  },

  //相当于computed
  getters: {
    filterList: state => state.list.filter(item => item > 5),
    set: state=> state.setting.set

  },

  //每一个modules 又是一个vuex
  modules: {
    user: {
      // namespaced:true,
      state: {
        special: 0
      },
      mutations: {
        changeSpecial(state) {
          state.special += 10
        }
      },
    },
    setting: {
      state: {
        set: 1
      }
    },
    userC: {
      namespaced: true,
      state: {
        num: 1
      },
      mutations: {
        changeNum(state) {
          state.num++
        }
      }
    }
  }
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  //store: store
  store,
  components: { App },
  template: "<App/>"
});
